﻿<div class="my-9">
    <JsonPre @bind-Value="@_queryStr" Class="rounded-2 emphasis"></JsonPre>
</div>

@code {
    [EditorRequired]
    [Parameter]
    public AlarmRuleUpsertViewModel AlarmRule { get; set; } = new();

    public string _queryStr = "Query";

    protected override void OnParametersSet()
    {
        var queryStr = string.IsNullOrEmpty(AlarmRule.WhereExpression) ? "{}" : AlarmRule.WhereExpression;
        var aggsStr = "{";
        for (int i = 0; i < AlarmRule.LogMonitorItems.Count; i++)
        {
            if (i>0)
            {
                aggsStr += ",";
            }

            var item = AlarmRule.LogMonitorItems[i];
            aggsStr += $"\"{item.Alias}\":{{\"value_count\":{{\"field\":\"{item.Field}\"}}}}";
        }
        aggsStr += "}";

        _queryStr = $"{{\"query\":{queryStr},\"aggs\":{aggsStr}}}";
    }
}
